package kg.aibek.dc;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;

public class Application {

	public static void main(String[] args) {

		int type = 13; // AdaptiveEncoding 1, ConditionalAdaptiveEncoding 2,
						// ConditionalAdaptiveEncoding2 3
						// AdaptiveDecoding = 11, ConditAdaptiveDecoding = 12,
						// ConditionalAdaptiveDecoding2 = 13

		String folderPath = "/home/aibek/Desktop/DC/assignment2/test2/";
		String orgFile = folderPath + "kafka_trial.txt";
		String encFile = folderPath + "encoded.txt";
		String enc2File = folderPath + "encoded2.txt";
		String enc3File = folderPath + "encoded3.txt";
		String decFile = folderPath + "decoded.txt";
		String dec2File = folderPath + "decoded2.txt";
		String dec3File = folderPath + "decoded3.txt";

		String rate1 = folderPath + "rate1.txt";
		String rate2 = folderPath + "rate2.txt";
		String rate3 = folderPath + "rate3.txt";
		ArithmeticEncoder encoder = null;
		ArithmeticDecoder decoder = null;

		try {
			String fileToWrite = "";
			String fileToRead = "";
			String rateFile = "";
			if (type == 1) {
				encoder = new AdaptiveEncoder();
				fileToWrite = encFile;
				rateFile = rate1;
			} else if (type == 2) {
				encoder = new ConditionalAdaptiveEncoder();
				fileToWrite = enc2File;
				rateFile = rate2;
			} else if (type == 3) {
				encoder = new ConditionalAdaptiveEncoder2();
				fileToWrite = enc3File;
				rateFile = rate3;
			} else if (type == 11) {
				decoder = new AdaptiveDecoder();
				fileToRead = encFile;
				fileToWrite = decFile;
			} else if (type == 12) {
				decoder = new ConditionalAdaptiveDecoder();
				fileToRead = enc2File;
				fileToWrite = dec2File;
			} else if (type == 13) {
				decoder = new ConditionalAdaptiveDecoder2();
				fileToRead = enc3File;
				fileToWrite = dec3File;
			}

			if (type / 10 == 0) {
				fileToRead = orgFile;
				System.out.println("Encoding file " + fileToRead + " ");
				System.out.println("Start time " + Calendar.getInstance().getTime());
				encoder.encode(fileToRead);
				System.out.println("Encoding file " + fileToRead + " is finished successfully");
				System.out.println("Finish time " + Calendar.getInstance().getTime());
				System.out.println("Compression ratio : " + encoder.getCompressionRate());

				writeToFile(encoder.getEncodedStr(), fileToWrite);

				writeRate(encoder.getRate(), rateFile);
			} else if (type / 10 == 1) {
				System.out.println("Decoding file " + fileToRead);
				System.out.println("Start time " + Calendar.getInstance().getTime());
				decoder.decode(fileToRead);
				System.out.println("Finish time " + Calendar.getInstance().getTime());

				writeToFile(decoder.getDecodedStr(), fileToWrite);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void writeToFile(String str, String filePath) {
		try {
			FileWriter fstream = new FileWriter(filePath);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(str);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void writeRate(Map<Integer, Double> rateMap, String filePath) {
		try {
			FileWriter fstream = new FileWriter(filePath);
			BufferedWriter out = new BufferedWriter(fstream);
			for (int i : rateMap.keySet()) {
				out.write(i + "\t" + rateMap.get(i) + "\n");
			}
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
